package com.pks.insurance.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.pks.insurance.dao.CustomerDAO;
import com.pks.insurance.dto.CustomerDto;
import com.pks.insurance.dto.VehicleDto;
import com.pks.insurance.util.HibernateUtil;

/**
 * Implementation class for RegistrationDAO
 * 
 * @author Pankaj Soni
 */

public class CustomerDAOImpl implements CustomerDAO {

	private static final Logger LOGGER = Logger
			.getLogger(CustomerDAOImpl.class);

	/**
	 * 
	 * @see com.CustomerDAO.autoInsurance.dao.RegistrationDAO#createCustomer(com.cts.autoInsurance
	 *      .dto.PersonalDetailsDto)
	 */
	@Override
	public final boolean createCustomer(final CustomerDto customerDtoReq) {
		LOGGER.debug("begin");
		boolean result = false;
		VehicleDto vehicleDto = new VehicleDto();
		vehicleDto.setSsn(customerDtoReq.getSsn());
		customerDtoReq.setVehicle(vehicleDto);
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(customerDtoReq);
			transaction.commit();
			result = true;
		} catch (HibernateException e) {
			transaction.rollback();
			LOGGER.error(e.getMessage());
		} finally {
			session.flush();
			session.close();
		}
		LOGGER.info("result: " + result);
		LOGGER.debug("end");
		return result;
	}

	/**
	 * 
	 * @see com.CustomerDAO.autoInsurance.dao.RegistrationDAO#isExistingUser(com.cts.
	 *      autoInsurance.dto.LoginDto)
	 */
	@Override
	public final boolean isExistingUser(final CustomerDto customerDtoReq) {
		LOGGER.debug("begin");
		boolean result = false;
		Session session = HibernateUtil.getSession();
		try {
			if (session.get(CustomerDto.class, customerDtoReq.getSsn()) != null)
				result = true;
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
		} finally {
			session.close();
		}
		LOGGER.info("result: " + result);
		LOGGER.debug("end");
		return result;
	}

	/**
	 * @see com.pks.autoInsurance.dao.CustomerDAO#updateCustomer(com.pks.autoInsurance.dto.CustomerDto)
	 */
	@Override
	public final boolean updateCustomer(CustomerDto customerDtoReq) {
		LOGGER.debug("begin");
		boolean result = false;
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			customerDtoReq.setVehicle((VehicleDto) session.load(
					VehicleDto.class, customerDtoReq.getSsn()));
			session.merge(customerDtoReq);
			transaction.commit();
			result = true;
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
			transaction.rollback();
		} finally {
			session.flush();
			session.close();
		}
		LOGGER.info("result: " + result);
		LOGGER.debug("end");
		return result;
	}

	/**
	 * @see com.pks.autoInsurance.dao.CustomerDAO#findCustomer(java.lang.String)
	 */
	@Override
	public final CustomerDto findCustomer(final String ssn) {
		LOGGER.debug("begin");
		Session session = HibernateUtil.getSession();
		CustomerDto customerDto = null;
		try {
			customerDto = (CustomerDto) session.get(CustomerDto.class, ssn);
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
		} finally {
			session.close();
		}
		LOGGER.info("customerDto: " + customerDto);
		LOGGER.debug("end");
		return customerDto;
	}

	/**
	 * @see com.pks.autoInsurance.dao.CustomerDAO#findCustomers()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public final List<CustomerDto> findCustomers() {
		LOGGER.debug("begin");
		List<CustomerDto> customers = null;
		Session session = HibernateUtil.getSession();
		try {
			customers = session.createQuery("from CustomerDto").list();
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
		} finally {
			session.close();
		}
		LOGGER.info("persons: " + customers);
		LOGGER.debug("end");
		return customers;
	}

	/**
	 * @see com.pks.autoInsurance.dao.CustomerDAO#findCustomers(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public final List<CustomerDto> findCustomers(final String ssn) {
		LOGGER.debug("begin");
		LOGGER.info("ssn: " + ssn);
		List<CustomerDto> customers = null;
		Session session = HibernateUtil.getSession();
		try {
			customers = session
					.createQuery("from CustomerDto where ssn like:ssn")
					.setString("ssn", "%" + ssn + "%").list();
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
		} finally {
			session.close();
		}
		LOGGER.info("persons: " + customers);
		LOGGER.debug("end");
		return customers;
	}

	/**
	 * @see com.pks.autoInsurance.dao.CustomerDAO#deleteCustomer(java.lang.String)
	 */
	@Override
	public final void deleteCustomer(final String ssn) {
		LOGGER.debug("begin");
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		CustomerDto customerDto = null;
		try {
			transaction = session.beginTransaction();
			Object object = session.get(CustomerDto.class, ssn);
			if (object != null) {
				customerDto = (CustomerDto) object;
				session.delete(customerDto);
			}
			transaction.commit();
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
			transaction.rollback();
		} finally {
			session.flush();
			session.close();
		}
		LOGGER.debug("end");
	}

	/**
	 * 
	 * @see com.cts.autoInsurance.dao.LoginDAO#authenticateUser(com.cts.autoInsurance
	 *      .dto.RegistrationDtoReq)
	 */
	@Override
	public final int authenticateUser(final CustomerDto customer) {
		LOGGER.debug("begin");
		Session session = HibernateUtil.getSession();
		final String sql = "from CustomerDto where ssn=:ssn AND passwd=:passwd";
		int result = INVALID_USER;
		try {
			Query query = session.createQuery(sql);
			query.setString("ssn", customer.getSsn());
			query.setString("passwd", customer.getPasswd());
			Object queryResult = query.uniqueResult();
			LOGGER.info("queryResult: " + queryResult);
			if (queryResult != null) {
				if ("adminadmin".equals(((CustomerDto) queryResult).getSsn())) {
					result = VALID_ADMIN;
				} else {
					result = VALID_USER;
				}
			}
		} catch (HibernateException e) {
			LOGGER.error(e.getMessage());
		} finally {
			session.close();
		}
		LOGGER.info("result: " + result);
		LOGGER.debug("end");
		return result;
	}
}
